﻿package {
	import AStar.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.setTimeout;
	import flash.net.*;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class Main extends MovieClip {

		var stageX:int;
		var stageY:int;
		var temp_stageX:int = 9;
		var temp_stageY:int = 9;
		
		var s_w:Sound;
		var s_l:Sound;
		var s_n:Sound;
		var s_b:Sound;
		
		private var channel:SoundChannel;

		var count = 0;
		var score = 0;

		var stageXY:Array = new Array(10);
		var map:Array = new Array(9);

		var astar:Astar;

		var g:MovieClip;

		var sp_m:Array = new Array();

		public function Main() {

			stop();
			Math.random();
			
			
			s_w = new mp3_w();
			s_l = new mp3_l();
			s_n = new mp3_n();
			s_b = new mp3_b();
			
			for (var i=0; i<10; i++) {
				stageXY[i] = [null,null,null,null,null,null,null,null,null,null];

				if (i==9) {
					map[i] = [1,1,1,1,1,1,1,1,1,1];
				} else {
					map[i] = [0,0,0,0,0,0,0,0,0,1];
				}
			}
			astar = new Astar();

			g = new gov();

			addSprite();
			addSprite();
			
			
			loadScore();
			
			sco.txt.text = score;
			reGameBtn.addEventListener('click',reGameBtnClickHandler);
			stage.addEventListener('click',mouseClickHandler);
		}
		function reGameBtnClickHandler(e:MouseEvent) {
			gameOver();
		}
		function mouseClickHandler(e:MouseEvent) {
			stageX = (e.stageX - 10)/40 ;
			stageY = (e.stageY - 10)/40 ;

			if (stageX > 8 || stageY >8) {
				return;
			}

			if ((stageX == temp_stageX)&&(stageY == temp_stageY)) {
				return;
			}
			

			if (MovieClip(stageXY[temp_stageY][temp_stageX])) {
				MovieClip(stageXY[temp_stageY][temp_stageX]).gotoAndStop(1);
			}
			if (stageXY[stageY][stageX]) {

				MovieClip(stageXY[stageY][stageX]).gotoAndPlay(2);
				temp_stageX=stageX;
				temp_stageY=stageY;
				if(channel) channel.stop();
				channel = s_l.play();

			} else {

				if (stageXY[temp_stageY][temp_stageX]) {

					var temp_movie = stageXY[temp_stageY][temp_stageX];

					//移动卡通
					//moveMovie(temp_movie,stageX,stageY,temp_stageX,temp_stageY);
					moveMovie(new Point(temp_stageY,temp_stageX),new Point(stageY,stageX),map,temp_movie);

				}
			}
			//打印数组
			/*for (var i=0; i<10; i++) {
			trace(map[i]);
			}
			trace("---------------------");*/
		}
		function addSprite() {

			if (count > 77) {
				gameOver();
				return;
			}
			
			
			for (var i=0; i < 3; i++) {
				var b = newSprite();
				
				do {
					stageY = Math.round(Math.random()*100) % 9;
					stageX = Math.round(Math.random()*100) % 9;
				} while (stageXY[stageY][stageX]);



				stageXY[stageY][stageX] = b;
				map[stageY][stageX] = 1;

				b.x = stageX*40+20;
				b.y = stageY*40+20;
				addChild(b);
				count++;

				line();
			}
		}
		
		function newSprite():MovieClip{
			
			if (sp_m[1]) removeChild(sp_m[1]);
			if (sp_m[2]) removeChild(sp_m[2]);
			if (sp_m[3]) removeChild(sp_m[3]);
			
			while(sp_m.length < 4){
				sp_m.push(getSprite());
			}
			
			
			sp_m[1].x = 400;
			sp_m[1].y = 30;
			sp_m[2].x = 440;
			sp_m[2].y = 30;
			sp_m[3].x = 480;
			sp_m[3].y = 30;
			
			addChild(sp_m[1]);
			addChild(sp_m[2]);
			addChild(sp_m[3]);
			
			return sp_m.shift();
			
		}
		
		function getSprite():MovieClip {
			var b:MovieClip = null;
			
			if (Math.round(Math.random()*100) % 15 == 1 ) {
				b = new white();
			} else {

				switch (Math.round(Math.random()*100) % 6) {
					case 0 :
						b = new red();
						break;
					case 1 :
						b = new blue();
						break;
					case 2 :
						b = new yellow();
						break;
					case 3 :
						b = new green();
						break;
					case 4 :
						b = new purple();
						break;
					case 5 :
						b = new cyan();
						break;
				}
			}
			b.buttonMode= true;

			return b;
		}
		function moveMovie(p1:Point,p2:Point,a:Array,m) {
			var temp_m:Array;


			astar.map = map;

			temp_m = astar.findWay(p1.x,p1.y,p2.x,p2.y);

			if (temp_m.length > 0) {
				stage.removeEventListener('click',mouseClickHandler);
				stageXY[temp_stageY][temp_stageX] = null;
				map[temp_stageY][temp_stageX] = 0;

				temp_stageX=9;
				temp_stageY=9;
				stageXY[stageY][stageX] = m;
				map[stageY][stageX] = 1;
				sleepMovie(m,temp_m,0);
			} else {
				if(channel) channel.stop();
				channel = s_b.play();
				m.gotoAndPlay(2);
			}

		}
		function sleepMovie(m,temp_m,i) {
			if (i == temp_m.length) {
				
				if(channel) channel.stop();
				
				if ( !line() ) {
					channel = s_n.play();
					addSprite();
				}else{
					channel = s_w.play();
				}

				stage.addEventListener('click',mouseClickHandler);
				return;
			}
			m.y = temp_m[i].x*40+20;
			m.x = temp_m[i].y*40+20;

			i++;

			flash.utils.setTimeout(function():void{
			sleepMovie(m,temp_m,i);
			},30,null);
		}
		function line():Boolean {//消除4个以上直线
			var bool:Boolean = false;
			var checkArray:Array =new Array(4);
			checkArray[0] = checkLine(1);
			checkArray[1] = checkLine(4);
			checkArray[2] = checkLine(5);
			checkArray[3] = checkLine(9);

			for (var j=0; j < 4; j++) {
				if (checkArray[j].length > 4) {

					for (var i=0; i < checkArray[j].length; i++) {
						var t = checkArray[j][i];

						if (stageXY[t.y][t.x]) {
							var m = stageXY[t.y][t.x];

							removeChild(m);
							count--;

							stageXY[t.y][t.x] = null;
							map[t.y][t.x] = 0;
						}
					}
					bool = true;
					score += checkArray[j].length * 3;
				}
			}
			sco.txt.text = score;
			return bool;
		}
		function checkLine(i:int):Array {//连线搜索
			var p1:Array = new Array;
			var p2:Array = new Array;
			var _t_x:int = 0;
			var _t_y:int = 0;

			switch (i) {
				case 1 :
					_t_x = 0;
					_t_y = 1;
					break;
				case 4 :
					_t_x = 1;
					_t_y = 0;
					break;
				case 5 :
					_t_x = 1;
					_t_y = 1;
					break;
				case 9 :
					_t_x = -1;
					_t_y = 1;
					break;
			}
			//trace (stageXY[stageY][stageX]);

			var m = stageXY[stageY][stageX].toString();
			var _m = [null,null,null,null,null,null];
			var t_y;
			var t_x;

			if (m != '[object white]') {
				_m = [m];
			} else {
				_m = ['[object red]','[object blue]','[object yellow]','[object green]','[object purple]','[object cyan]'];
			}
			//trace (_m.length);
			for (var i=0; i < _m.length; i++) {
				m= _m[i];
				p1 = [];
				p2 = [];
				t_y = stageY;
				t_x = stageX;

				while ((t_x += _t_x) >= 0  && (t_y += _t_y) >= 0 ) {
					if (stageXY[t_y][t_x] && (stageXY[t_y][t_x].toString() == m  || stageXY[t_y][t_x].toString() ==  '[object white]')) {
						p1.push(new Point(t_x,t_y));
					} else {
						break;
					}
				}
				t_y = stageY;
				t_x = stageX;

				while ((t_x -= _t_x) >= 0  && (t_y -= _t_y) >= 0 ) {
					if (stageXY[t_y][t_x] && (stageXY[t_y][t_x].toString() == m || stageXY[t_y][t_x].toString() ==  '[object white]')) {
						p2.push(new Point(t_x,t_y));
					} else {
						break;
					}
				}
				p2.push(new Point(stageX,stageY));
				
				if((p1.length + p2.length) > 4) break;
			}
			
			
			return p1.concat(p2);
		}
		
		function gameOver() {
			g.x=275;
			g.y=200;
			g.txt.text=score;
			g.overButton.addEventListener('click',reGameClickHandler);
			g.scoreButton.addEventListener('click',scoreClickHandler);
			
			addChild(g);

			//trace("Game Over");
			for (var i=0; i<10; i++) {
				for (var j=0; j<10; j++) {
					var t_m = stageXY[i][j];
					if (t_m) {
						removeChild(t_m);
						stageXY[i][j] = null;
					}
				}
			}
			for (var t=0; t<10; t++) {

				if (i==9) {
					map[t] = [1,1,1,1,1,1,1,1,1,1];
				} else {
					map[t] = [0,0,0,0,0,0,0,0,0,1];
				}
			}
			
			reGameBtn.removeEventListener('click',reGameBtnClickHandler);
			stage.removeEventListener('click',mouseClickHandler);
		}
		function scoreClickHandler(e:MouseEvent){
			var url:URLRequest = new URLRequest("http://127.0.0.1/topScore.php");
			var values:URLVariables =new URLVariables();
			//trace ("http://127.0.0.1/topScore.PHP?n=" + g.imputTxt.text + "&s=" + g.txt.text);
			values.n=g.imputTxt.text;
			values.s=g.txt.text;
			url.data= values;
			new URLLoader(url).addEventListener(Event.COMPLETE ,function(e:Event){
					reGame();
				});
		}
		function reGameClickHandler(e:MouseEvent) {
			reGame();
		}
		function reGame() {
			g.overButton.removeEventListener('click',reGameClickHandler);
			removeChild(g);
			count = 0;
			score = 0;
			
			addSprite();
			addSprite();
			
			loadScore();
			
			sco.txt.text = score;
			
			//scoreButton.removeEventListener('click',scoreClickHandler);
			reGameBtn.addEventListener('click',reGameBtnClickHandler);
			stage.addEventListener('click',mouseClickHandler);
		}
		
		function loadScore()
		{
			var XML_URL = 'http://127.0.0.1/score.xml?random='+Math.random()*789456+'&';
			var myXMLURL:URLRequest  = new URLRequest(XML_URL);
			var myLoader:URLLoader = new URLLoader(myXMLURL);
			
			myLoader.addEventListener(Event.COMPLETE,
				function (e:Event){
					var myXML =  XML(myLoader.data);
					/*trace("数据完成");
					trace(myXML);
					trace("-------------");
					trace(myXML.user[0].@name);*/
					
					topScore.user1.text = myXML.user[0].@name;
					topScore.sco1.text = myXML.user[0].@sco;
					topScore.user2.text = myXML.user[1].@name;
					topScore.sco2.text = myXML.user[1].@sco;
					topScore.user3.text = myXML.user[2].@name;
					topScore.sco3.text = myXML.user[2].@sco;
					
			 	});
		}
		
	}//class
}//package